package com.youi.shop.config;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * 自动装配类
 * 1，默认配置
 * 2，类的声明
 * 3，加载自定义配置
 */
@Configuration
@EnableSwagger2
@EnableConfigurationProperties(com.youi.shop.config.SwaggerProperties.class)  //通过注解的方式告诉springboot加载自定义配置
//@ConditionalOnProperty(prefix = "mytest.swagger"
//        , value = "enable", matchIfMissing = true)
public class SwaggerAutoConfiguration {

    public SwaggerAutoConfiguration() {

        System.out.println("--------------------自定义的Swagger自动装配类生效！！！--------------------");
    }

    @Autowired
    private SwaggerProperties swaggerProperties ;


    @Bean
    public Docket docketA() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("aaa");
    }

    @Bean
    public Docket createAPI() {
        return new Docket(DocumentationType.SWAGGER_2)
                .forCodeGeneration(true)
                .select()
                .apis(RequestHandlerSelectors.any())
                //过滤生成链接
                .paths(PathSelectors.any())
                .build()
                .groupName("xxf")
                .apiInfo(apiInfo());
    }

//    private ApiInfo apiInfo() {
//        Contact contact = new Contact("bear的电商项目", "http://baidu.com"
//                , "#");
//        ApiInfo apiInfo = new ApiInfoBuilder()
//                .license("Apache License Version 2.0")
//                .title("中国好电商api文档")
//                .description("这是个神奇的网站")
//                .contact(contact)
//                .version("1.0")
//                .build();
//        return apiInfo;
//    }

    private ApiInfo apiInfo() {
        Contact contact = new Contact("xxf的电商项目", "http://baidu.com"
                , "xifangbear@gmail.com");
        ApiInfo apiInfo = new ApiInfoBuilder()
                .license("Apache License Version 2.0")
                .title(swaggerProperties.getTitle())
                .description(swaggerProperties.getDescription())
                .contact(contact)
                .version("1.0")
                .build();
        return apiInfo;
    }
}
